﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessTier.Facade.Interface;
using BusinessTier.DataContext;
using Domain;

namespace BusinessTier.Facade.Implementation
{
    public class CounterService : ICounterService
    {
        public void AddPrintCounterEntry(PrinterCounter counter)
        {
            using (var ctx = new SimplyData())
            {
                counter.Date = DateTime.Now;
                ctx.PrinterCounters.Add(counter);
                ctx.SaveChanges();
            }
        }

        public IEnumerable<PrinterCounter> GetAllCounterEntries()
        {
            using (var ctx = new SimplyData())
            {
                return ctx.PrinterCounters.OrderByDescending(p => p.Date).ToList();
            }
        }


        public PrinterCounter GetLastCounterEntry()
        {
            using (var ctx = new SimplyData())
            {
                var counters = ctx.PrinterCounters.OrderByDescending(p => p.Date);
                if(counters.Count() == 0)
                {
                    return null;
                }
                return counters.First();
            }
        }
    }
}
